#include "common/modules.h"
#include "common/settings.h"
#include "mvc/view/main_dialog.h"
#include "ui_main_dialog.h"

#include <QProcess>
#include <QStringList>
#include <QSettings>

Main_dialog::Main_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Main_dialog)
{

    ui->setupUi(this);
    QImage image(":images/GeParTra.png");
    ui->image->setPixmap(QPixmap::fromImage(image));
    ui->image->setScaledContents(true);
    setModal(true);
    initialize_modules();
    QSize windowSize;
    windowSize.setHeight(440);
    windowSize.setWidth(545);

    this->setFixedSize(windowSize);
}

Main_dialog::~Main_dialog()
{
    delete ui;
}

// PRIVATE SLOTS

/**
  * Emit the new project signal to the view component.
  */
void Main_dialog::send_new_project_signal()
{
    emit s_new_project(selected_module);
}

/**
  * Emit the new project signal to the view component.
  */
void Main_dialog::new_project(QModelIndex index)
{
    std::cout << "New Project " << index.row() << std::endl;
    if(index.row() == 0)
        selected_module = get_module_value(uavs_jw_module);
    else if(index.row() == 1)
        selected_module = get_module_value(conventional_w_module);
    else if(index.row() == 2)
        selected_module = get_module_value(bwb_module);

    send_new_project_signal();
}

/**
  * Displays the description of the selected module.
  */
void Main_dialog::show_description(int module)
{
    //std::cout << "TODO: Show Description" << std::endl;
    QString description;
    QSettings settings("tflic", "uavs");

    if(module == get_module_value(uavs_jw_module))
        description = settings.value(tr("uav_desc")).toString();
    else if(module == get_module_value(conventional_w_module))
        description = settings.value(tr("mav_desc")).toString();
    else if(module == get_module_value(bwb_module))
        description = settings.value(tr("muav_desc")).toString();

    ui->description->setText(description);
}

/**
  *
  */
void Main_dialog::activate_new_button(QModelIndex index)
{
    if(index.row() == 0)
        selected_module = get_module_value(uavs_jw_module);
    else if(index.row() == 1)
        selected_module = get_module_value(conventional_w_module);
    else if(index.row() == 2)
        selected_module = get_module_value(bwb_module);

    show_description(selected_module);
    ui->new_project->setEnabled(true);
}

//
// PRIVATE METHODS
//

/**
  * Initialize the three selectable modules buttons: UAVS, Conventional, BWB
  */
void Main_dialog::initialize_modules()
{
    model = new QStandardItemModel(0);
    QStandardItem *item1 = new QStandardItem(QIcon(":images/uavicon.png"), "Joined Wings");
    item1->setEditable(false);

    QStandardItem *item2 = new QStandardItem(QIcon(":images/plane.png"), "Conventional");
    item2->setEditable(false);

    QStandardItem *item3 = new QStandardItem(QIcon(":images/flyingicon.png"), "BWB/Flying Wing");
    item3->setEditable(false);

    model->appendRow(item1);
    model->appendRow(item2);
    model->appendRow(item3);

    ui->modules_list->setViewMode(QListView::IconMode);
    ui->modules_list->setMovement(QListView::Static);
    ui->modules_list->setIconSize(QSize(80, 80));
    ui->modules_list->setModel(model);
    ui->modules_list->setGeometry(QRect(0, 0, 240, 320));

    connect(ui->modules_list, SIGNAL(clicked(QModelIndex)), this, SLOT(activate_new_button(QModelIndex)));
    connect(ui->modules_list, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(new_project(QModelIndex)));
    connect(ui->new_project, SIGNAL(clicked()), this, SLOT(send_new_project_signal()));
}
